import { createMemoryHistory, createRouter } from "vue-router";
import Layout from "../layout/index.vue";
import Index from "../pages/index.vue";
import NotFound from "../pages/404.vue";
import Login from "../pages/login.vue";

// 商品管理页面
import GoodsList from "../pages/goods/list.vue";
import CategoryList from "../pages/category/list.vue";
import SkusList from "../pages/skus/list.vue";
import CouponList from "../pages/coupon/list.vue";
// 用户管理
import UserList from "../pages/user/list.vue";
import LevelList from "../pages/level/list.vue";

// 订单管理
import OrderList from "../pages/order/list.vue";
import CommentList from "../pages/comment/list.vue";

// 管理员管理
import ManagerList from "../pages/manager/list.vue";
import AccessList from "../pages/access/list.vue";
import RoleList from "../pages/role/list.vue";

// 系统设置
import BaseSetting from "../pages/setting/base.vue";
import BuySetting from "../pages/setting/buy.vue";
import ShipSetting from "../pages/setting/ship.vue";

// 分销模块
import Distribution from "../pages/distribution/index.vue";
import DistributionSetting from "../pages/distribution/setting.vue";

// 其他模块
import ImageList from "../pages/image/list.vue";
import NoticeList from "../pages/notice/list.vue";

import { createWebHashHistory } from "vue-router";

// 默认路由，即所有用户都拥有的路由
const routes = [
  {
    path: "/",
    name: "layout",
    component: Layout,
    children: []
  },
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: {
      title: "登录页",
    },
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound
  }
];

// 动态路由的基础数据(全量数据)，后面会根据后台给用户赋予的权限来从中选取部分或全部数据添加到用户的动态路由中
const asyncRoutes = [
  {
    name: "/",
    path: "/",
    component: Index,
    meta: {
      title: "首页",
    }
  },
  {
    name: "/goods/list",
    path: "/goods/list",
    component: GoodsList,
    meta: {
      title: "商品管理",
    }
  },
  {
    name: "/category/list",
    path: "/category/list",
    component: CategoryList,
    meta: {
      title: "分类管理",
    }
  },
  {
    name: "/skus/list",
    path: "/skus/list",
    component: SkusList,
    meta: {
      title: "规格管理",
    }
  },
  {
    name: "/coupon/list",
    path: "/coupon/list",
    component: CouponList,
    meta: {
      title: "优惠劵管理",
    }
  },
  {
    name: "/user/list",
    path: "/user/list",
    component: UserList,
    meta: {
      title: "用户管理",
    }
  },
  {
    name: "/level/list",
    path: "/level/list",
    component: LevelList,
    meta: {
      title: "级别管理",
    }
  },
  {
    name: "/order/list",
    path: "/order/list",
    component: OrderList,
    meta: {
      title: "订单管理",
    }
  },
  {
    name: "/comment/list",
    path: "/comment/list",
    component: CommentList,
    meta: {
      title: "评论管理",
    }
  },
  {
    name: "/manager/list",
    path: "/manager/list",
    component: ManagerList,
    meta: {
      title: "管理员管理",
    }
  },
  {
    name: "/access/list",
    path: "/access/list",
    component: AccessList,
    meta: {
      title: "权限管理",
    }
  },
  {
    name: "/role/list",
    path: "/role/list",
    component: RoleList,
    meta: {
      title: "角色管理",
    }
  },
  {
    name: "/setting/base",
    path: "/setting/base",
    component: BaseSetting,
    meta: {
      title: "系统设置",
    }
  },
  {
    name: "/setting/buy",
    path: "/setting/buy",
    component: BuySetting,
    meta: {
      title: "交易设置",
    }
  },
  {
    name: "/setting/ship",
    path: "/setting/ship",
    component: ShipSetting,
    meta: {
      title: "物流设置",
    }
  },
  {
    name: "/distribution/index",
    path: "/distribution/index",
    component: Distribution,
    meta: {
      title: "分销管理",
    }
  },
  {
    name: "/distribution/setting",
    path: "/distribution/setting",
    component: DistributionSetting,
    meta: {
      title: "分销设置",
    }
  },
  {
    name: "/image/list",
    path: "/image/list",
    component: ImageList,
    meta: {
      title: "图库管理",
    }
  },
  {
    name: "/notice/list",
    path: "/notice/list",
    component: NoticeList,
    meta: {
      title: "公告管理",
    }
  }
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 是否添加了新路由
let hasNewRoutes = false 
// 添加动态路由的方法，即根据传入的菜单数组, 匹配路由基础数据，将符合条件的赋予给用户作为动态路由
export function addRoutes(menu) {
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      let item = asyncRoutes.find((o) => o.path == e.frontpath)
      if (item && !router.hasRoute(item.path)) {
        router.addRoute("layout", item)
        hasNewRoutes = true
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child)
      }
    })
  }
  findAndAddRoutesByMenus(menu)
  return hasNewRoutes
}
